package mt.business;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import mt.model.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Json on 16/1/27.
 */
public class Cache {

    private static Map<String,UserVO> CodeUserMap = new HashMap<String,UserVO>();
    private static Map<Integer,UserVO> UserIdMap = new HashMap<Integer,UserVO>();

    public static UserVO PutUserVO(UserVO userVO){
        CodeUserMap.put(userVO.getCode(),userVO);
        UserIdMap.put(userVO.getUserId(),userVO);
        return userVO;
    }

    public static UserVO GetUserVOByCode(String code){
        return CodeUserMap.get(code);
    }
    public static UserVO GetUserVOById(int id){
        return UserIdMap.get(id);
    }

    public static void Init(){
        List<Record> userList = Db.find("select userId,userName,wxcode,openId from mt_user ");
        System.out.println("Cache Init UserList.size : " + userList.size());
        for (Record user:userList ){
            UserVO userVO = new UserVO();
            userVO.setUserId(user.getInt("userId"));
            userVO.setUserName(user.getStr("userName"));
            userVO.setCode(user.getStr("wxcode"));
            userVO.setOpenId(user.getStr("openId"));
            Cache.PutUserVO(userVO);
        }
    }



}
